package com.google.code.flabbyraffle;

import java.util.*;

import static java.util.Arrays.asList;

public class Raffle {

    private int jackpotAmount;
    private LotteryNumberGenerator lotteryNumberGenerator;
    private WinningNumbers winningNumbers;
    private TicketList ticketList;

    public Raffle() {

    }

    public void uploadTickets(String ticketBatch) {
        this.ticketList = new TicketList(ticketBatch);
    }

    public void performDrawingFor(int jackpotAmount) {
        this.jackpotAmount = jackpotAmount;
        winningNumbers = new WinningNumbers(lotteryNumberGenerator.generateWinningNumbers());
    }

    public Integer ticketWinningsFor(Integer ticketId) {
        int totalJackpotWon = 0;
        for (Integer ticketNumber : ticketNumbersFor(ticketId)) {
            totalJackpotWon += jackpotAmount * winningNumbers.containsNumber(ticketNumber);
        }
        return totalJackpotWon;
    }

    private List<Integer> ticketNumbersFor(Integer ticketId) {
        return ticketList.getByTicketId(ticketId).getTicketNumbers();
    }

    public void setLotteryNumberGenerator(LotteryNumberGenerator lotteryNumberGenerator) {
        this.lotteryNumberGenerator = lotteryNumberGenerator;
    }
}
